<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>未成年子女信息添加</title>
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <link href="/static/css/resource.css" rel="stylesheet" type="text/css" />
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
    <link href="/static/css/default.css" rel="stylesheet" type="text/css" />
    <style>
        .discell {
            text-align:right;
            width:20%;
        }
        .input {
            width:35%;
        }
    </style>
</head>
<body>
<div class="mini-fit">
    <div style="height:100%;width: 100%;overflow-x: auto;overflow-y: auto;">
        <form id="form1" method="post">
            <input name="id" id="id" class="mini-hidden" style="width: 100%" readonly />
            <input name="prinum" id="prinum" class="mini-hidden" style="width: 100%" readonly />
            <br />
            <table align="center">
                <tr>
                    <td class="discell"><font color="#dc143c">*</font>姓&emsp;&emsp;名:</td>
                    <td class="input">
                        <input name="name" id="name" class="mini-textbox" required="true" emptyText="请输入姓名" />
                    </td>
                    <td class="discell">性&emsp;&emsp;别:</td>
                    <td class="input">
                        <input name="sex" id="sex" class="mini-combobox"
                               textField="codeName" valueField="codeId" emptyText="请选择..."
                               url="/azbj/inmates/baseinfo/getTbsysCode?codeType=2_2&pcodeId=-1&sn=1"
                        />
                    </td>
                </tr>
                <tr>
                    <td class="discell"><font color="#dc143c">*</font>证件类型:</td>
                    <td class="input">
                        <input name="idType" id="idType" class="mini-combobox" required="true" onvaluechanged="onIdTypeMouseOut()"
                               textField="codeName" valueField="codeId" emptyText="请选择..." onclick="change()"
                               url="/azbj/inmates/baseinfo/getTbsysCode?codeType=2_13&pcodeId=-1&sn=1" />
                    </td>
                    <td class="discell"><font color="#dc143c" id="font">*</font>证件号码:</td>
                    <td class="input">
                        <input name="znsfid" id="znsfid" class="mini-textbox" required="true" onvaluechanged="onMouseOut()" emptyText="请先选择证件类型" />
                    </td>
                </tr>
                <tr>
                    <td><input class="mini-hidden" name="num" id="num"></td>
                </tr>
                <tr>
                    <td class="discell">出生日期:</td>
                    <td class="input">
                        <input property="editor" class="mini-datepicker" id="age" name="age" allowInput="false" emptyText="请选择出生日期" ondrawdate="onDrawDate"/>
                    </td>
                    <td class="discell">监护人情况:</td>
                    <td class="input">
                        <input name="jianhuren" id="jianhuren" class="mini-combobox" emptyText="请选择"
                               data="[{ id: ''+'01', name: '有监护人可以履责' },{ id: ''+'02', name: '有监护人无法履责' },{ id: ''+'03', name: '无监护人' }]"
                               textField="name" valueField="id" />
                    </td>

                </tr>
                <tr>
                    <td class="discell">失&emsp;&emsp;学:</td>
                    <td class="input">
                        <input name="shixue" id="shixue" class="mini-combobox" emptyText="请选择"
                               data="[{ id: 1, name: '是' },{ id: 2, name: '否' }]"
                               textField="name" valueField="id" />
                    </td>
                    <td class="discell">流浪社会:</td>
                    <td class="input">
                        <input name="liulang" id="liulang" class="mini-combobox" emptyText="请选择"
                               data="[{ id: 1, name: '是' },{ id: 2, name: '否' }]"
                               textField="name" valueField="id" />
                    </td>
                </tr>
                <tr>
                    <td class="discell">生活困难程度:</td>
                    <td class="input">
                        <input name="kunnan" id="kunnan" class="mini-combobox" emptyText="请选择"
                               data="[{ id: ''+'01', name: '一般困难' },{ id:  ''+'02', name: '特别困难' },{ id:  ''+'03', name: '不困难' }]"
                               textField="name" valueField="id" />
                    </td>
                    <td class="discell">违法犯罪行为:</td>
                    <td colspan="3">
                        <input name="fanzui" id="fanzui" class="mini-combobox" emptyText="请选择"
                               data="[{ id: 1, name: '有' },{ id: 2, name: '无' }]"
                               textField="name" valueField="id" required="false" />
                    </td>
                </tr>
                <tr>
                    <td class="discell">
                        户籍地:
                    </td>
                    <td colspan="3">
                        <input id="nativepro" name="nativepro" class="mini-combobox" required="false" emptyText="省份" style="width:180px;"
                               textfield="codeName" valueField="codeId"
                               onclick="onDeptChanged('nativepro')" onvaluechanged="onValueChanged('nativepro')" />
                        <input id="nativecity" name="nativecity" class="mini-combobox" required="false" emptyText="市" style="width: 180px"
                               textfield="codeName" valueField="codeId" onclick="onDeptChanged('nativecity')"
                               onvaluechanged="onValueChanged('nativecity')" />
                        <input id="nativecou" name="nativecou" class="mini-combobox" required="false" emptyText="县" style="width: 170px"
                               textfield="codeName" valueField="codeId" onclick="onDeptChanged('nativecou')"
                               onvaluechanged="onValueChanged('nativecou')" />
                        <input id="nativevill" name="nativevill" class="mini-combobox" required="false" emptyText="街道" style="width: 150px"
                               textfield="codeName" valueField="codeId"onclick="onDeptChanged('nativevill')" />
                    </td>
                </tr>
                <tr>
                    <td class="discell"></td>
                    <td colspan="3">
                        <input class="mini-textbox" required="false" style="width: 690px" emptyText="详细地址" id="nativenum" name="nativenum" />
                    </td>
                </tr>
                <tr>
                    <td class="discell">
                        居住地:
                    </td>
                    <td colspan="3" class="input">
                        <input id="placepro" name="placepro" class="mini-combobox" required="false" emptyText="省份" style="width:180px;"
                               textfield="codeName" valueField="codeId"
                               onclick="onLiveChanged('placepro')" onvaluechanged="onChanged('placepro')" />
                        <input id="placecity" name="placecity" class="mini-combobox" required="false" emptyText="市" style="width: 180px"
                               textfield="codeName" valueField="codeId" onclick="onLiveChanged('placecity')"
                               onvaluechanged="onChanged('placecity')" />
                        <input id="placecou" name="placecou" class="mini-combobox" required="false" emptyText="县" style="width: 170px"
                               textfield="codeName" valueField="codeId" onclick="onLiveChanged('placecou')"
                               onvaluechanged="onChanged('placecou')" />
                        <input id="placevill" name="placevill" class="mini-combobox" required="false" emptyText="街道" style="width: 150px"
                               textfield="codeName" valueField="codeId"onclick="onLiveChanged('placevill')" />
                    </td>
                </tr>
                <tr>
                    <td class="discell"></td>
                    <td colspan="3">
                        <input class="mini-textbox" style="width: 690px" emptyText="详细地址" id="placenum" required="false" name="placenum" />
                    </td>
                </tr>
            </table>
        </form>
        <div style="padding:2px;margin: 0 auto;text-align: center">
            <a class="mini-button" iconCls="fa-save" plain="true" style="font-size: 15px" onclick="saveData()">保存</a>
            <a class="mini-button" iconCls="fa-close" plain="true" style="font-size: 15px" onclick="onCancel()">关闭</a>
        </div>
    </div>
</div>
<#--司法部分配各省 上报人员的统一标识-->
<input name="PriDepartNum" class="mini-hidden" />
<#--罪犯编号-->
<input name="PriNum" class="mini-hidden" />
<script type="text/javascript">
    mini.parse();
    var form = new mini.Form("form1");
    var id = mini.get("id");
    var prinum = mini.get("prinum");
    var znsfid = mini.get("znsfid");
    var nativepro = mini.get("nativepro");
    var nativecity = mini.get("nativecity");
    var nativecou = mini.get("nativecou");
    var nativevill = mini.get("nativevill");
    var nativenum = mini.get("nativenum");
    var placepro = mini.get("placepro");
    var placecity = mini.get("placecity");
    var placecou = mini.get("placecou");
    var placevill = mini.get("placevill");
    var placenum = mini.get("placenum");
    var idType = mini.get("idType");

    //标准方法接口定义
    function saveData() {
        form.validate();
        if (form.isValid() == false) {
            return;
        }
        var data = form.getData();
        var json = mini.encode(data);
        if (!onMouseOut()) {
            return;
        }
        $.ajax({
            url: "/azbj/social/noadult/save",
            data: {data: json},
            type: "post",
            success: function (success) {
                if (success.code == 1) {
                    id.setValue(success.data);
                    mini.alert("保存成功", "信息提示", function () {
                        onCancel();
                    });
                } else if (success.code == 5599) {
                    mini.alert(success.message);
                } else {
                    mini.alert("保存失败");
                }
            }
        });
    }

    /**
     * 户籍地址
     * @param e
     */
    var urltemp = "/azbj/getCodeDetail/?codeType=2_1&codeId=-2";
    function onDeptChanged(e) {
        if ("nativepro" == e) {
            var value = nativepro.getValue();
            nativepro.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=-1";
            nativepro.setUrl(url);
            if (value != '') {
                nativepro.setValue(value);
            } else {
                nativepro.select(0);
            }
        }
        if ("nativecity" == e) {
            var value = nativecity.getValue();
            if (nativepro.getValue() == "") {
                nativepro.setUrl(urltemp);
                nativepro.setValue("");
                nativecou.setUrl(urltemp);
                nativecou.setValue("");
                nativevill.setUrl(urltemp);
                nativevill.setValue("");
                return;
            }
            var codeId = nativepro.getValue();
            nativecity.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=" + codeId;
            nativecity.setUrl(url);
            if (value != '') {
                nativecity.setValue(value);
            } else {
                nativecity.select(0);
            }
        }

        if ("nativecou" == e) {
            var value = nativecou.getValue();
            if (nativecity.getValue() == "") {
                nativecou.setUrl(urltemp);
                nativecou.setValue("");
                nativevill.setUrl(urltemp);
                nativevill.setValue("");
                return;
            }
            var codeId = nativecity.getValue();
            nativecou.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=" + codeId;
            nativecou.setUrl(url);
            if (value != '') {
                nativecou.setValue(value);
            } else {
                nativecou.select(0);
            }
        }

        if ("nativevill" == e) {
            var value = nativevill.getValue();
            if (nativecou.getValue() == "") {
                nativevill.setUrl(urltemp);
                nativevill.setValue("");
                return;
            }
            var parent_code = nativecou.getValue();
            nativevill.setValue("");
            var url = "/azbj/getCodeDetail?codeType=2_1&codeId=" + parent_code;
            nativevill.setUrl(url);
            if (value != '') {
                nativevill.setValue(value);
            } else {
                nativevill.select(0);
            }
        }
    }

    function onValueChanged(e) {
        if ("nativepro" == e) {
            nativecity.setValue("");
            nativecou.setValue("");
            nativevill.setValue("");
        }
        if ("nativecity" == e) {
            nativecou.setValue("");
            nativevill.setValue("");
        }
        if ("nativecou" == e) {
            nativevill.setValue("");
        }
    }

    /**
     * 居住地址
     * placepro  placecity placecou placevill placenum
     * @param e
     */
    function onLiveChanged(e) {
        if ("placepro" == e) {
            var value = placepro.getValue();
            placepro.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=-1";
            placepro.setUrl(url);
            if (value != '') {
                placepro.setValue(value);
            } else {
                placepro.select(0);
            }
        }
        if ("placecity" == e) {
            var value = placecity.getValue();

            if (placepro.getValue() == "") {
                placepro.setUrl(urltemp);
                placepro.setValue("");
                placecou.setUrl(urltemp);
                placecou.setValue("");
                placevill.setUrl(urltemp);
                placevill.setValue("");
                return;
            }
            var codeId = placepro.getValue();
            placecity.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=" + codeId;
            placecity.setUrl(url);
            if (value != '') {
                placecity.setValue(value);
            } else {
                placecity.select(0);
            }
        }

        if ("placecou" == e) {
            var value = placecou.getValue();

            if (placecity.getValue() == "") {
                placecou.setUrl(urltemp);
                placecou.setValue("");
                placevill.setUrl(urltemp);
                placevill.setValue("");
                return;
            }
            var codeId = placecity.getValue();
            placecou.setValue("");
            var url = "/azbj/getCodeDetail/?codeType=2_1&codeId=" + codeId;
            placecou.setUrl(url);
            if (value != '') {
                placecou.setValue(value);
            } else {
                placecou.select(0);
            }
        }

        if ("placevill" == e) {
            var value = placevill.getValue();

            if (placecou.getValue() == "") {
                placevill.setUrl(urltemp);
                placevill.setValue("");
                return;
            }
            var parent_code = placecou.getValue();
            placevill.setValue("");
            var url = "/azbj/getCodeDetail?codeType=2_1&codeId=" + parent_code;
            placevill.setUrl(url);
            if (value != '') {
                placevill.setValue(value);
            } else {
                placevill.select(0);
            }
        }
    }

    function onChanged(e) {
        if ("placepro" == e) {
            placecity.setValue("");
            placecou.setValue("");
            placevill.setValue("");
        }
        if ("placecity" == e) {
            placecou.setValue("");
            placevill.setValue("");
        }
        if ("placecou" == e) {
            placevill.setValue("");
        }
    }

    window.onload = function () {
        prinum.setValue(${prinum});
        mini.get("znsfid").setEnabled(false);
    }
    /**
     * 未成年子女证件类型去掉部分证件--警官证/士兵证等
     */
    function change() {
        var data = mini.get("idType").getData();
        for (var i = data.length-1; i >=0 ; i--) {
            if (data[i].codeId === '3'
                || data[i].codeId === '4'
                || data[i].codeId === '5'
                || data[i].codeId === '6'
                || data[i].codeId === '7'
                || data[i].codeId === '12'
                || data[i].codeId === '13'
            ) {
                data.splice(i,1);
            }
        }
        mini.get("idType").setData(data);
    }

    function onIdTypeMouseOut() {
        var idType = mini.get("idType");
        var idNum = mini.get("znsfid");
        var num = mini.get("num");
        //如果证件类型选择'无'
        if (idType.getValue() === '98') {
            idNum.setEmptyText("无证件号码，不用填写");
            $("#font").hide();
            idNum.required = false;
            $("#idNum").toggleClass("mini-textbox-disabled mini-textbox-readOnly");
            idNum.setEnabled(false);
            num.setValue(idNum.getValue());
            idNum.setValue('无');
        } else {
            //如果证件类型选择其他
            if (idNum.getValue() === '' && (num.getValue() !== '' || num.getValue() !== null)) {
                idNum.setValue(num.getValue());
            }
            idNum.setEmptyText("请填写证件号码");
            idNum.setEnabled(true);
            $("#font").show();
            idNum.required = true;
        }
    }

    function onMouseOut() {
        var id = mini.get("znsfid").getValue();
        if (idType.getValue() === "1" || idType.getValue() === "2") {
            if (id.length === 18 && onIDCardsValidation(id)) {
                var age = id.substring(6, 10) + "-" + id.substring(10, 12) + "-" + id.substring(12, 14);
                mini.get("age").setValue(age);
                if(id.length == 18) {
                    var num = id[16];
                    if(num%2 ==0){
                        mini.get("sex").setValue("2");
                    } else {
                        mini.get("sex").setValue("1");
                    }
                } else if(id.length == 15) {
                    var num = id[14];
                    if(num%2 ==0){
                        mini.get("sex").setValue("2");
                    } else {
                        mini.get("sex").setValue("1");
                    }
                }
                //对身份证校验 今天日期减去18年 那个日期
                var date = new Date();
                var yearInput = id.substring(6,10);
                var monthInput = id.substring(10,12);
                var dayInput = id.substring(12,14);
                var dateInputString = yearInput + '-' + monthInput + '-' + dayInput;
                var dateInput = new Date(dateInputString);
                var time = Math.abs(date.getTime() - dateInput.getTime())/1000/24/60/60;
                var year = Math.floor(time/365);
                if (year >= 18) {
                    mini.alert("该人员已成年请勿录入");
                    return false;
                }
            } else {
                mini.alert("请输入正确的身份证号码");
                return false;
            }
        }
        return true;
    }

    /**
     * 社会关系 -- 点击新增/编辑未成年子女信息 -- 保证只能选择18岁之内的出生日期
     * @param e
     */
    function onDrawDate(e) {
        var date = e.date;
        var d = new Date();
        var d2 = d.getDate()-(18*366);
        //18年前的日期时间戳
        var previousDayTime = d.setDate(d2);
        if (date.getTime() < previousDayTime) {
            e.allowSelect = false;
        }
        var now1 = new Date();
        if (date.getTime() > now1.getTime()) {
            e.allowSelect = false;
        }
    }
</script>
</body>
</html>